package com.mindskip.xzs.controller.admin;


import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.Phase;
import com.mindskip.xzs.domain.enums.PhaseStatusEnum;
import com.mindskip.xzs.domain.enums.UserStatusEnum;
import com.mindskip.xzs.service.PhaseService;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.admin.education.PhasePageResponseVM;
import com.mindskip.xzs.viewmodel.admin.education.PhaseResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController("AdminEducationPhaseController")
@RequestMapping(value = "/api/admin/education")
public class PhaseController extends BaseApiController {

    private final PhaseService phaseService;

    @Autowired
    public PhaseController(PhaseService phaseService) {
        this.phaseService = phaseService;
    }

    @RequestMapping(value = "/phase/list", method = RequestMethod.POST)
    public RestResponse<List<Phase>> list() {
        List<Phase> phases = phaseService.allPhase();
        return RestResponse.ok(phases);
    }

    @RequestMapping(value = "/phase/treeList", method = RequestMethod.POST)
    public RestResponse<List<Phase>> treeList() {
        List<Phase> phases = phaseService.newAllPhase();
        return RestResponse.ok(phases);
    }

    @RequestMapping(value = "/phase/parentPhase", method = RequestMethod.POST)
    public RestResponse<List<Phase>> parentPhase() {
        List<Phase> phases = phaseService.parentPhase();
        return RestResponse.ok(phases);
    }

    @RequestMapping(value = "/phase/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<PhaseResponseVM>> pageList(@RequestBody PhasePageResponseVM model) {
        PageInfo<Phase> pageInfo = phaseService.page(model);
        PageInfo<PhaseResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> modelMapper.map(e, PhaseResponseVM.class));
        return RestResponse.ok(page);
    }

    @RequestMapping(value = "/phase/edit", method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid PhaseResponseVM model) {
        Phase phase = modelMapper.map(model, Phase.class);
        if (model.getId() == null) {
            phaseService.insertByFilter(phase);
        } else {
            phaseService.updateByIdFilter(phase);
        }
        return RestResponse.ok();
    }

    @RequestMapping(value = "/phase/select/{id}", method = RequestMethod.POST)
    public RestResponse<PhaseResponseVM> select(@PathVariable Integer id) {
        Phase phase = phaseService.selectById(id);
        PhaseResponseVM vm = modelMapper.map(phase, PhaseResponseVM.class);
        return RestResponse.ok(vm);
    }

    @RequestMapping(value = "/phase/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        phaseService.deleteById(id);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/phase/changeStatus/{id}", method = RequestMethod.POST)
    public RestResponse<Integer> changeStatus(@PathVariable Integer id) {
        Phase phase = phaseService.selectById(id);
        PhaseStatusEnum phaseStatusEnum = PhaseStatusEnum.fromCode(phase.getStatus());
        Integer newStatus = phaseStatusEnum == PhaseStatusEnum.Enable ? UserStatusEnum.Disable.getCode() : UserStatusEnum.Enable.getCode();
        phase.setStatus(newStatus);
        phaseService.updateById(phase);
        return RestResponse.ok(newStatus);
    }
}
